//
//  LLPersonMianViewController.m
//  UserUse
//
//  Created by Yiwi on 16/10/12.
//  Copyright © 2016年 CJPC. All rights reserved.
//

#import "LLPersonMianViewController.h"
#import "LLPersonMainView.h"
#import "LoginViewController.h"
#import "LLModificationViewController.h"
#import "LLOrderViewController.h"
#import "SystermMessageViewController.h"
#import "LLAboutViewController.h"
#import "LLShareManager.h"

const NSInteger  JUMP_MYORDER = 0;//我的订单
const NSInteger  JUMP_MESSAGE = 1;//系统消息
const NSInteger  JUMP_ABOUT = 2;//关于我们

@interface LLPersonMianViewController ()<LLPersonDelegate,LLLoginDelegate>{
    LLPersonMainView * _personMianView;
}

@property(nonatomic,strong)LLShareManager *shareManager;

@end

@implementation LLPersonMianViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title =@"个人中心";
    self.navigationItem.rightBarButtonItem  = [[UIBarButtonItem alloc] initWithCustomView:[self barButtonItemWithImage:[UIImage imageNamed:@"share"] highImage:[UIImage imageNamed:@"share"] target:self action:@selector(shareButtonClick)]];

}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    LLUserModel * user = [LLUserHandle getUserData];
    [self setupSubViewWith:user];
}
-(void)setupSubViewWith:(LLUserModel*)user{
        _personMianView = [LLPersonMainView new];
        _personMianView.frame = kScreenFrame;
        _personMianView.delegate = self;
        _personMianView.user = user;
        [self.view addSubview:_personMianView];
        [_personMianView setupSubView];
        [_personMianView updateTopViewWithUser:user];
}
-(UIView*)barButtonItemWithImage:(UIImage *)image highImage:(UIImage *)highImage target:(id)target action:(SEL)action{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setBackgroundImage:image forState:UIControlStateNormal];
    [btn setBackgroundImage:highImage forState:UIControlStateHighlighted];
    [btn sizeToFit];
    btn.size = CGSizeMake(16, 17);
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    UIView *containView = [[UIView alloc] initWithFrame:btn.bounds];
    [containView addSubview:btn];
    return containView;
}
-(void)shareButtonClick{
    [self.shareManager show];
}
#pragma mark -LLPersonTableViewDelegate

-(void)didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%ld",indexPath.row);
    if (indexPath.row == JUMP_MYORDER) {
        //如果未登录跳到登录页先登录
        if([LLUserHandle getUserData] == nil){
            LoginViewController * loginVC = [LoginViewController new];
            [self.navigationController pushViewController:loginVC animated:NO];
            return;
        }
        LLOrderViewController * orderVC = [LLOrderViewController new];
        [self.navigationController pushViewController:orderVC animated:NO];
    }else if (indexPath.row == JUMP_MESSAGE){
        SystermMessageViewController * messageVC = [SystermMessageViewController new];
        [self.navigationController pushViewController:messageVC animated:NO];
    }else if (indexPath.row == JUMP_ABOUT){
        LLAboutViewController * aboutVC = [LLAboutViewController new];
        [self.navigationController pushViewController:aboutVC animated:NO];
    }
}
//登陆
-(void)login{
    LoginViewController * loginViewController = [LoginViewController new];
    loginViewController.delegate = self;
    [self.navigationController pushViewController:loginViewController animated:NO];
}
//跳到身份完善页
-(void)jumpDetail{
    LLModificationViewController *modificationVC = [LLModificationViewController new];
    modificationVC.isChange = YES;
    [self.navigationController pushViewController:modificationVC animated:NO];
}
//退出登陆
-(void)exitLogin{
    //清理本地缓存后刷新页面
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"退出登录"
                                                                   message:@""
                                                            preferredStyle:UIAlertControllerStyleAlert];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"退出"
                                              style:UIAlertActionStyleDestructive
                                            handler:^(UIAlertAction *action) {
                                              
                                                [LLUserHandle clearUserData];
                                                [_personMianView updateTopViewWithUser:[LLUserHandle getUserData]];
                                                LoginViewController *login = [[LoginViewController alloc]init];
                                                login.delegate = self;
                                                [self.navigationController pushViewController:login animated:YES];
                                            }]];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"不退出"
                                              style:UIAlertActionStyleCancel
                                            handler:^(UIAlertAction *action) {
                                      
                                            }]];
    [self presentViewController:alert animated:YES completion:nil];
   
}
//登录成功后回调
#pragma mark -LLLoginDelegate
-(void)loginSucessWithUser:(LLUserModel *)user{
    [self.view clearView];
    [self setupSubViewWith:user];
}
-(LLShareManager *)shareManager{
    if (!_shareManager) {
        _shareManager = [LLShareManager new];
    }
    return _shareManager;
}
@end
